package com.example.demo.jenkins;

import com.example.demo.utils.LoggerConfig;
import com.offbytwo.jenkins.JenkinsServer;
import com.offbytwo.jenkins.client.JenkinsHttpClient;

import java.net.URI;
import java.net.URISyntaxException;

public class JenkinsConnect {

    // https://blog.csdn.net/qq_32641153/article/details/94230465

    private static final String JENKINS_URL = "http://127.0.0.1/jenkins";

    private static final String JENKINS_USERNAME = "admin";

    private static final String JENKINS_PASSWORD = "123456";

    private static JenkinsServer jenkinsServer;

    private static JenkinsHttpClient jenkinsHttpClient;

    /**
     * 获取连接
     * @return JenkinsHttpClient连接信息
     */
    public static JenkinsHttpClient getClient(){
        try {
            jenkinsHttpClient = new JenkinsHttpClient(new URI(JENKINS_URL), JENKINS_USERNAME, JENKINS_PASSWORD);
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
        return jenkinsHttpClient;
    }

    /**
     * 获取Jenkins连接对象
     * @return 返回server
     */
    public static JenkinsServer connectJenkins(){
        try {
            jenkinsServer = new JenkinsServer(new URI(JENKINS_URL), JENKINS_USERNAME, JENKINS_PASSWORD);
        } catch (URISyntaxException e) {
            LoggerConfig.getLoggerError("jenkins connect error");
            e.printStackTrace();
        }
        return jenkinsServer;
    }
}
